// Created by Joe on 2017/7/20.

/**
 * @verCodeCheck/fastLogin/passwordLogin/changePassword:
 *      operate successfully if valueCode equals 1
 * @register/findPassword/certification:
 *      operate successfully if code equals 0000
 */

app.service(
    'ProfileService',
    [
        'config', 'RequestProxyService', 'UserService',
        function (config, RequestProxyService, UserService) {
            return {
                verCodeCheck: function (mobile, verifyCode, type) {
                    type = type || 'common';
                    var urlMap = {
                        register: ['/cust/checkUserIsNew', {mobile: mobile, verifyCode: verifyCode, inviteCode: ''}],
                        findPassword: ['/cust/getUserPassword', {mobile: mobile, verifyCode: verifyCode}],
                        common: ['/cust/checkVerifyCode', {mobile: mobile, verifyCode: verifyCode, inviteCode: ''}]
                    };
                    return RequestProxyService.requestProxy(urlMap[type][0], {}, urlMap[type][1],
                        function (resolve, info) {
                            try {
                                var rst = info.result[0];
                                resolve({msg: rst.msg, valueCode: rst.valueCode});
                            } catch (e) {
                                resolve({msg: '验证失败', valueCode: '0'});
                            }
                        }, 'user')();
                },
                getUserDetail: function () {
                    return RequestProxyService.requestProxy('/cust/detail', {}, {},
                        function (resolve, info) {
                            resolve(info);
                        }, 'user', true)();
                },
                generateVerCode: function (mobile, type) {
                    return RequestProxyService.requestProxy('/cust/getCode', {},
                        {mobilePhone: mobile, type: type},
                        function (resolve, info) {
                            resolve(info || {});
                        }, 'user', true)();
                },
                register: function (mobile, password) {
                    return RequestProxyService.requestProxy('/cust/passWordRegister', {},
                        {mobile: mobile, passWord: password, deviceToken: config.deviceToken},
                        function (resolve, info) {
                            if (info.result[0]) UserService.accessToken = info.result[0].access_token;
                            resolve(info);
                        }, 'user')();
                },
                fastLogin: function (mobile, verifyCode) {
                    return RequestProxyService.requestProxy('/cust/quicklyLogin', {},
                        {mobile: mobile, verifyCode: verifyCode, state: '', deviceToken: config.deviceToken},
                        function (resolve, info) {
                            try {
                                var rst = info.result[0];
                                if (rst.valueCode === '1') UserService.accessToken = rst.access_token;
                                resolve({msg: rst.msg, valueCode: rst.valueCode});
                            } catch (e) {
                                resolve({msg: '登录失败', valueCode: '0'});
                            }
                        }, 'user')();
                },
                passwordLogin: function (mobile, password) {
                    return RequestProxyService.requestProxy('/cust/passWordLogin', {},
                        {mobile: mobile, passWord: password, deviceToken: config.deviceToken},
                        function (resolve, info) {
                            try {
                                var rst = info.result[0];
                                if (rst.valueCode === '1') UserService.accessToken = rst.access_token;
                                resolve({msg: rst.msg, valueCode: rst.valueCode});
                            } catch (e) {
                                resolve({msg: '登录失败', valueCode: '0'});
                            }
                        }, 'user')();
                },
                findPassword: function (mobile, password) {
                    return RequestProxyService.requestProxy('/cust/findPassword', {},
                        {mobile: mobile, passWord: password, deviceToken: config.deviceToken},
                        function (resolve, info) {
                            if (info.result[0]) UserService.accessToken = info.result[0].access_token;
                            resolve(info);
                        }, 'user')();
                },
                changePassword: function (oldPassword, newPassword) {
                    return RequestProxyService.requestProxy('/cust/changePassword', {},
                        {oldPassWord: oldPassword, newPassWord: newPassword},
                        function (resolve, info) {
                            try {
                                var rst = info.result[0];
                                resolve({msg: rst.msg, valueCode: rst.valueCode});
                            } catch (e) {
                                resolve({msg: '密码修改失败', valueCode: '0'});
                            }
                        }, 'user')();
                },
                certification: function (mobile, name, card) {
                    return RequestProxyService.requestProxy('/realNameAuthent/phoneCheck', {},
                        {realName: name, idCard: card, mobile: mobile},
                        function (resolve, info) {
                            resolve(info);
                        }, 'user')();
                }
            };
        }
    ]
);